package com.wm.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wm.system.domain.TCity;
import com.wm.system.domain.TCounty;
import com.wm.system.domain.TProvince;
import com.wm.system.mapper.TProvinceMapper;
import com.wm.system.service.ITCityService;
import com.wm.system.service.ITCountyService;
import com.wm.system.service.ITProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 省份信息表 服务实现类
 * </p>
 *
 * @author hyq
 * @since 2023-10-23 11:14:43
 */
@Service
public class TProvinceServiceImpl extends ServiceImpl<TProvinceMapper, TProvince> implements ITProvinceService {

    @Autowired
    private ITCityService cityService;

    @Autowired
    private ITCountyService countyService;

    @Override
    public List<TProvince> selectProvinceList() {

        List<TProvince> provinceList = this.list();
        List<TCity> cityList = cityService.list();
        List<TCounty> countyList = countyService.list();

        Map<String, List<TCounty>> countyMap = countyList.stream().collect(Collectors.groupingBy(TCounty::getCityCode));
        cityList.forEach(c->{
            List<TCounty> tCounties = countyMap.get(c.getCode());
            c.setCountyList(tCounties);
        });
        Map<String, List<TCity>> cityMap = cityList.stream().collect(Collectors.groupingBy(TCity::getProvinceCode));

        provinceList.forEach(e->{
            List<TCity> tCities = cityMap.get(e.getCode());
            e.setCityList(tCities);
        });
        return provinceList;
    }
}
